package com.treasure.mall.zbqmallapi.controller.integral;

import com.treasure.mall.biz.entity.integral.UserIntegralLogPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.integral.IntegralService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbqmallapi.vo.integral.IntegralLogVO;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/25
 */
@RestController
@RequestMapping("/integral")
public class IntegralController {
    @Resource
    private IntegralService integralService;

    @GetMapping("/user/logs")
    public ApiResult integralLogs(
            @CurrentUser UserPO userPO,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size
    ) {
        Page<UserIntegralLogPO> pages = integralService.logsByUser(userPO.getUserId(), page, size);
        PageDataVO<IntegralLogVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageCount(pages.getTotalPages());
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageSize(size);
        pageDataVO.setTotal(pages.getTotalElements());

        List<IntegralLogVO> vos = pages.getContent().stream().map(IntegralLogVO::new).collect(Collectors.toList());
        pageDataVO.setRows(vos);

        return ApiResult.ok(pageDataVO);
    }
}
